package com.atguigu.srb.sms.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.srb.common.util.RandomUtils;
import com.atguigu.srb.sms.service.SmsService;
import com.atguigu.srb.sms.util.SmsProperties;
import com.google.gson.Gson;
import lombok.SneakyThrows;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    RedisTemplate redisTemplate;

    @SneakyThrows
    @Override
    public void send(String mobile) {
        //配置,公共参数
        DefaultProfile defaultProfile = DefaultProfile.getProfile( SmsProperties.REGION_Id,
                SmsProperties.KEY_ID,
                SmsProperties.KEY_SECRET);
        //api的客户端
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);
        //请求参数封装
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setSysMethod(MethodType.POST);
        commonRequest.setSysDomain("dysmsapi.aliyuncs.com");
        commonRequest.setSysVersion("2017-05-25");
        commonRequest.setSysAction("SendSms");
        commonRequest.putQueryParameter("RegionId", SmsProperties.REGION_Id);
        commonRequest.putQueryParameter("PhoneNumbers", mobile);
        commonRequest.putQueryParameter("SignName", SmsProperties.SIGN_NAME);
        commonRequest.putQueryParameter("TemplateCode", SmsProperties.TEMPLATE_CODE);
        //放入验证码
        String fourBitRandom = RandomUtils.getFourBitRandom();
        Map<String,String> map = new HashMap<>();
        map.put("code",fourBitRandom);
//        Gson gson = new Gson();
//        String json = gson.toJson(map);
        commonRequest.putQueryParameter("TemplateParam", JSON.toJSONString(map));
        //客户端发送请求
        CommonResponse commonResponse = iAcsClient.getCommonResponse(commonRequest);

        //处理请求返回结果
        int httpStatus = commonResponse.getHttpStatus();
        HttpResponse httpResponse = commonResponse.getHttpResponse();
        System.out.println(httpStatus);
        System.out.println(httpResponse);

        //根据短信发送的结果判断是否将验证码存入redis中
        if(httpStatus==200){
            redisTemplate.opsForValue().set("srb:sms:code:"+mobile,fourBitRandom);
        }


    }
}
